<template>
	<div>
		<view>
			<nav-bar title="编辑资料"></nav-bar>
			<view :style="{'margin-top':navHeight+'px'}">
				<view class="header">
					<view class="bg"></view>
					<image class="portraint" :src="userInfo.headImage?userInfo.headImage:'https://oss.jxhecong.com/v2/image/protrait_default@2x.png'" @click.stop="uploadImg"></image>
				</view>
				<view class="content" :style="{'margin-top':contentOffTop+'px'}">
					<view class="input">
						<view>昵称</view>
						<input placeholder="请输入您的昵称" maxlength="20" v-model="userInfo.nickName" />
					</view>
					<view class="save" @click="save">保存</view>
				</view>
			</view>
		</view>
	</div>
</template>

<script>
	var globalData = getApp({
		allowDefault: true
	}).globalData
	import navBar from '../components/navBar.vue'
	import * as meApi from '../../api/user/me.js'
	import * as userApi from '../../api/user/index.js'
	import * as fileApi from '../../api/file/index.js'
	export default {
		components:{navBar},
		data(){
			return {
				navHeight:0,
				heartH:0,
				portraintOffTop:0,
				contentOffTop:0,
				userInfo:{
					nickName:'',
					headImage:''
				}
			}
		},
		onLoad() {
			this.navHeight = globalData.titleHeight;
			let userInfo=uni.getStorageSync('userInfo')
			this.getUserInfo();
		},
		methods:{
			modify_Click(){
				uni.navigateTo({
					url:'/pages/mine/modifyInfo'
				})
			},
			uploadImg(){
				let _this=this
				uni.showActionSheet({
					itemList:['从手机相册','拍照'],
					success(res) {
						uni.chooseImage({
							count:1,
							sizeType: ['original', 'compressed'],
							sourceType: [res.tapIndex==0?'album':'camera'],
							success: function (response) {
							        console.log(response);
									_this.uploadImageClick(response.tempFilePaths[0])
							    }
						})
					}
				})
			},
			// 获取用户信息
			uploadImageClick(imagePath){
		
				uni.showLoading({
					title:'正在上传'
				})
				fileApi.uploadFile(imagePath).then((res)=>{
					console.log("上传成功---")
					uni.hideLoading()
					if(res.code==0){
						this.userInfo.headImage = res.data.imgUrl
					}
					else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			
			updateUserInfo(userInfo) {
				uni.showLoading({
					title:'正在提交'
				})
				meApi.updateMemberInfo(userInfo.nickName,userInfo.headImage).then((res)=>{
					console.log(res.code)
					uni.hideLoading()
					if(res.code==0){
						uni.showToast({
							title:'更新成功'
						})
					}
					else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			
			getUserInfo(){
				userApi.getUserInfo().then((res) => {	
					if (res.code == 0) {		
						this.userInfo = res.data
					}		
				}).catch((errMsg) => {
				
				})
			},

			
			// 保存
			save(){
				if(this.userInfo.nickName.length==0){
					uni.showToast({
						title:'请输入昵称',
						icon:'none'
					})
					return
				}
				this.updateUserInfo(this.userInfo)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header{
		height:406rpx;
	}
	.header .bg{
		position: absolute;
		display: block;
		height:406rpx;
		width: 100%;
		background-color: #6ABF6C;
	}
	.header .portraint{
		position: absolute;
		width: 102px;
		height: 102px;
		border-radius: 51px;
		margin-top: 64px;
		left: 50%;
		transform: translateX(-50%);
	}
	
	.content{
		margin-top: 134px;
		padding: 15px;
		background-color: white;
	}
	
	.input{
		display: flex;
		align-items: center;
		flex-direction: row;
		justify-content: flex-start;
		height: 50px;
		border-bottom: solid 1rpx #DDDDDD;
		view{
			color: #333333;
			font-size: 16px;
		}
		input{
			flex: 1;
			text-align: right;
		}
	}
	
	.save{
		color: white;
		height: 45px;
		line-height: 45px;
		text-align: center;
		background: #6ABF6C;
		box-shadow: 0px 1px 2px 0px rgba(8, 139, 120, 0.3);
		border-radius: 50px;
		margin-top: 47px;
	}
	
</style>

